package com.happle.gcmclient.utility;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

import com.happle.gcmclient.config.CommonUtilities;
import com.happle.gcmclient.config.Constants;
import com.happle.gcmclient.obj.ConversationObj;
import com.happle.gcmclient.obj.ConversationsNotSyncObj;
import com.happle.gcmclient.obj.LngObj;
import com.happle.gcmclient.obj.MessageObj;

public final class ParameterUtil {

    public static JSONObject createUserDataParamsJSON (String registration_id,String username, String user_pwd) {
        JSONObject json = new JSONObject();
        try {
            json.put(Constants.registration_id_p, registration_id);
            json.put(Constants.phone_type_p, Constants.PHONE_TYPE);
            json.put(Constants.username_p, username);
            json.put(Constants.user_pwd_p, user_pwd);
        }catch (Exception e) {
        e.printStackTrace();
    }
    return json;
    }

    public static JSONObject createMessageParamsJSON (MessageObj msgObj) {
        JSONObject json = new JSONObject();
        try {
            json.put(Constants.msg_id_local_p, msgObj.get_msg_id_local());
            json.put(Constants.wave_id_p, msgObj.get_wave_id());
            json.put(Constants.msg_order_p,
                    String.valueOf(msgObj.get_msg_order()));
            json.put(Constants.is_owner_p,
                    String.valueOf(msgObj.is_is_owner()));
            json.put(Constants.lng_id_p, msgObj.get_lng_id());
            json.put(Constants.message_p, msgObj.get_message());
            json.put(Constants.datetime_p,
                    String.valueOf(System.currentTimeMillis()));
            json.put(Constants.msg_status_p,
                    String.valueOf(msgObj.is_msg_status() == true ? 1 : 0));
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    public static JSONObject createLanguageDataParamsJSON(LngObj lngObj) {
        JSONObject json = new JSONObject();
        try {
            json.put(Constants.lng_id_p, lngObj.get_lng_id());
            json.put(Constants.lng_name_p, lngObj.get_lng_name());
            json.put(Constants.is_marked_p,
                String.valueOf(lngObj.is_is_marked()));
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    public static  JSONObject createConversationParamsJSON (ConversationObj conversationObj, int conv_status) {
        JSONObject json = new JSONObject();
        try {
            json.put(Constants.first_msg_id_p,
                    conversationObj.get_first_msg_id());
            json.put(Constants.wave_id_p, conversationObj.get_wave_id());
            json.put(Constants.is_owner_p, conversationObj.is_is_owner());
            json.put(Constants.first_message_p,
                    String.valueOf(conversationObj.get_message()));
            json.put(Constants.lng_id_p, conversationObj.get_lng_id());
            json.put(Constants.datetime_p,
                    String.valueOf(conversationObj.get_datetime()));
            json.put(Constants.conv_status_p,
                    String.valueOf(conversationObj.is_conv_status()));
            json.put(Constants.msgs_count_p, String.valueOf(conversationObj.get_msgs_count()));
            json.put(Constants.conv_status_server_p, String.valueOf(conv_status));
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

	public static JSONObject createLoginRegistrationParamsJSON(
			String registration_id, String username, String user_pwd) {
		JSONObject json = new JSONObject();
		try {
            json.put(Constants.user_data_p, createUserDataParamsJSON(registration_id, username, user_pwd));
			json.put(Constants.user_status_p, Constants.STATUS_ON);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return json;
	}

	public static JSONObject createChangeStatusParamsJSON(
			String registration_id, String username, String user_pwd,
			int user_status) {
		JSONObject json = new JSONObject();
		try {
            json.put(Constants.user_data_p, createUserDataParamsJSON(registration_id,username, user_pwd));
			json.put(Constants.user_status_p, user_status);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return json;
	}

	public static JSONObject CreateSendMessageParamsJSON(String registration_id,
			String username, String user_pwd, MessageObj msgObj) {
		JSONObject json = new JSONObject();
		try {
            json.put(Constants.message_obj_p, createMessageParamsJSON(msgObj));
            json.put(Constants.user_data_p, createUserDataParamsJSON(registration_id,username, user_pwd));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return json;
	}

	public static JSONObject CreateDownloadConversationParamsJSON(String regId,
			int last_conversation_id, int more_conversation_number) {
		JSONObject json = new JSONObject();
		try {
			json.put(Constants.registration_id_p, regId);
			json.put(Constants.wave_id_p, String.valueOf(last_conversation_id));
			json.put(Constants.msgs_count_p,
					String.valueOf(more_conversation_number));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return json;
	}


    public static  JSONObject createUpdateConversationParamsJSON (ConversationObj conversationObj, int conv_status,String registration_id,String username, String user_pwd) {
        JSONObject json = new JSONObject();
        try {
            json = createConversationParamsJSON(conversationObj, conv_status);
            json.put(Constants.user_data_p, createUserDataParamsJSON(registration_id,username, user_pwd));
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    public static  JSONObject createDeleteConversationParamsJSON (ConversationObj conversationObj, String registration_id,String username, String user_pwd) {
        JSONObject json = new JSONObject();
        try {
            json.put(Constants.wave_id_p, conversationObj.get_wave_id());
            json.put(Constants.user_data_p, createUserDataParamsJSON(registration_id,username, user_pwd));
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

	public static JSONObject CreateUpdateLanguageParamsJSON(LngObj lngObj,
			String registration_id, String username, String user_pwd) {
		JSONObject json = new JSONObject();

		try {
            json.put(Constants.user_data_p, createUserDataParamsJSON(registration_id,username, user_pwd));
			json.put(Constants.language_p, createLanguageDataParamsJSON(lngObj));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return json;
	}

	public static JSONObject createSyncParamsJSON(Context mContext,
			ArrayList<LngObj> listLngObj, ArrayList<MessageObj> listNewsObj,
			ArrayList<ConversationsNotSyncObj> listUpdateObj,String registration_id, String username, String user_pwd) {

		    JSONObject syncDataObjectJson = new JSONObject();
		    JSONArray arrLanguage = new JSONArray();
		    JSONArray arrNewMsgsObj = new JSONArray();
		    JSONArray arrUpdateConvObj = new JSONArray();

		try {

			String regId = CommonUtilities.getInstance(mContext)
					.pullPreferences(mContext, CommonUtilities.REGISTRATION_ID);

            syncDataObjectJson.put(Constants.user_data_p, createUserDataParamsJSON(regId,"", ""));

			JSONObject tempObj = null;
			for (LngObj item : listLngObj) {
				tempObj = new JSONObject();
                tempObj = createLanguageDataParamsJSON(item);
				arrLanguage.put(tempObj);
			}

			for (MessageObj item : listNewsObj) {
				tempObj = new JSONObject();
                tempObj = createMessageParamsJSON(item);
                arrNewMsgsObj.put(tempObj);
			}

			for (ConversationsNotSyncObj item : listUpdateObj) {
				tempObj = new JSONObject();
				tempObj.put(Constants.wave_id_p, item.get_wave_id());
                tempObj.put(Constants.lng_id_p, item.get_lng_id());
				tempObj.put(Constants.is_owner_p, item.is_is_owner());
				tempObj.put(Constants.conv_status_p,
						String.valueOf(item.get_conv_status()));
                arrUpdateConvObj.put(tempObj);
			}

            syncDataObjectJson.put("languages", arrLanguage);
            syncDataObjectJson.put("new_msgs", arrNewMsgsObj);
            syncDataObjectJson.put("update_conv", arrUpdateConvObj);
            syncDataObjectJson.put(Constants.user_data_p, createUserDataParamsJSON(registration_id,username, user_pwd));

		} catch (Exception e) {
			e.printStackTrace();
		}
		Log.d("Sync JSON Object ", syncDataObjectJson.toString());

		return syncDataObjectJson;
	}
}
